package com.definesys.smecrailtransit.module.SiteDetails.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.BR;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.ItemEleStatusBinding;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.ELeStatusBean;
import com.definesys.smecrailtransit.module.SiteDetails.dialog.SiteReaErrorDialog;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by xupeizuo on 2017/8/21.
 */

public class SiteDetailsRealTimeAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {

    private Context mContext;
    private ArrayList<ELeStatusBean> arrayList;
    private SiteDetailsPresenter presenter;
    private HashMap<String,Object> map=new HashMap<>();
    private SiteReaErrorDialog siteReaErrorDialog;
    private int inWay=0;

    public SiteDetailsRealTimeAdapter(Context context, SiteDetailsPresenter siteDetailsPresenter,int way,SiteReaErrorDialog dialog){
        mContext=context;
        presenter=siteDetailsPresenter;
        arrayList=new ArrayList<>();
        this.inWay=way;
        this.siteReaErrorDialog=dialog;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemEleStatusBinding itemEleStatusBinding=DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_ele_status,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemEleStatusBinding.getRoot());
        smecBaseRecyclerViewHolder.setBinding(itemEleStatusBinding);
        smecBaseRecyclerViewHolder.setViewType(viewType);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        holder.getBinding().setVariable(BR.eleStatus,arrayList.get(position));
        ItemEleStatusBinding itemEleStatusBinding=(ItemEleStatusBinding)holder.getBinding();
        _signImg(arrayList.get(position).getStatus(),itemEleStatusBinding,position);
        holder.setIsRecyclable(false);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setArrayList(ArrayList<ELeStatusBean> list) {
        if(CommonUtils.notEmpty(list)){
            arrayList.clear();
            arrayList.addAll(list);
            notifyDataSetChanged();
        }
    }

    private void _signImg(String status,ItemEleStatusBinding itemEleStatusBinding,int position){
        if(status == null || status.equals("")){
            itemEleStatusBinding.eleStatus.setVisibility(View.INVISIBLE);
            itemEleStatusBinding.eleError.setVisibility(View.GONE);
            itemEleStatusBinding.eleErr.setClickable(false);
        }
        if(status.equals("异常")){
            itemEleStatusBinding.eleError.setVisibility(View.VISIBLE);
            itemEleStatusBinding.eleErr.setOnClickListener(v -> _showWindow(arrayList.get(position)));
        }else {
            itemEleStatusBinding.eleError.setVisibility(View.GONE);
            itemEleStatusBinding.eleErr.setClickable(false);
        }

        if(status.equals("异常")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_error));
        }else if(status.equals("上行")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_up));
        }else if(status.equals("下行")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_down));
        }else if(status.equals("停止")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_power));
        }else if(status.equals("待机")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_stop));
        }else if(status.equals("运行")){
            itemEleStatusBinding.eleStatus.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ele_none));
        }
    }

    private void _showWindow(ELeStatusBean eLeStatusBean){
        map.put("eleContractNo",eLeStatusBean.getEleContractNo());
        map.put("workorderId",eLeStatusBean.getWorkorderId());
        presenter.getErrorContent(map);
        if(siteReaErrorDialog == null){
            siteReaErrorDialog=new SiteReaErrorDialog(mContext,R.style.project_back,true,null);
        }
        if(inWay == 1){
            siteReaErrorDialog.frontToOpposite();
            return;
        }
        siteReaErrorDialog.show();
    }
}
